package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.lang.TypeReference;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.constant.AdaPayConfigType;
import com.tbit.uqbike.object.pojo.AdaPayConfig;
import com.tbit.uqbike.object.pojo.UserDivide;
import com.tbit.uqbike.service.business.AdaPayConfigService;
import com.tbit.uqbike.webmanager.dao.core.AdaPayConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdaPayConfigServiceImpl
 * @Description
 * @date 2021/10/21 14:44
 * @Version 1.0
 */
@Service("AdaPayConfigService")
public class AdaPayConfigServiceImpl implements AdaPayConfigService {
    @Autowired
    private AdaPayConfigDao adaPayConfigDao;
    @Resource
    private WeixinPayConfigServiceImpl weixinPayConfigService;

    @Override
    public AdaPayConfig getInfoByAccountId(Integer accountId) {
        return adaPayConfigDao.getInfoByAccountId(accountId);
    }

    @Override
    public AdaPayConfig getInfoByAccountIdNew(AdaPayConfig adaPayConfig) {
        return adaPayConfigDao.getInfoByAccountIdNew(adaPayConfig);
    }

    @Override
    public Integer addAdaPayConfig(AdaPayConfig adaPayConfig) {
        //记录商户变更日志
        Map<String, Object> requestBody = JSON.parseObject(JSON.toJSONString(adaPayConfig), new TypeReference<Map<String, Object>>() {
        });
        weixinPayConfigService.addLog(adaPayConfig.getAccountId(), 2, adaPayConfig.getOpId(), requestBody);
        /*if (adaPayConfigDao.getInfoByAccountId(adaPayConfig.getAccountId())!=null){
            throw new BaseException(SystemCodeEnum.ADAPAYCONFIG_HAVE_EXIST);
        }*/
        if (adaPayConfig.getConfigType().equals(AdaPayConfigType.WEIXIN_PROGRAM)) {
            adaPayConfig.setPay_channel("wx_lite");
        } else if (adaPayConfig.getConfigType().equals(AdaPayConfigType.ZFB_PROGRAM)) {
            adaPayConfig.setPay_channel("alipay_lite");
        } else if (adaPayConfig.getConfigType().equals(AdaPayConfigType.ZFB_APP)) {
            adaPayConfig.setPay_channel("alipay");
        }
        return adaPayConfigDao.addAdaPayConfig(adaPayConfig);
    }

    @Override
    public Integer addRatio(UserDivide userDivide) {
        return adaPayConfigDao.addRatio(userDivide);
    }

    @Override
    public Integer upRatioState(UserDivide userDivide) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", userDivide.getAccountId());
        map.put("userNo", userDivide.getUserNO());
        map.put("state", userDivide.getState());
        map.put("proportion", userDivide.getProportion());
        return adaPayConfigDao.upRatioState(map);
    }

    @Override
    public List<UserDivide> getByAccountId(Integer accountId, Integer rowCount, Integer pageNO) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        return adaPayConfigDao.getByAccountId(map);
    }

    @Override
    public UserDivide getRatioInfoById(String memberId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userNo", memberId);
        return adaPayConfigDao.getRatioInfoById(map);
    }

    @Override
    public Integer delRatioInfo(Integer accountId) {
        return adaPayConfigDao.delRatioInfo(accountId);
    }


}
